/**
*	首页
**/
app.controller('gIndexCtrl', ['$scope', '$state', '$stateParams', '$http', '$interval', '$cookieStore', 'utils',
  function($scope, $state, $stateParams, $http, $interval, $cookieStore, utils) {
    // 首页页面数据
    $scope.indexData = {
      bannerList: [],
      zxList: [],
      quickWarning: [],
      expoList: []
    }
    $scope.url = window.location.href

    $scope.swiper = {};

    // 页面初始配置
    $scope.pageInitData = {
      bannerLoading: true,
    }


    // 首页资讯列表
    utils.get('indexBannerList', {
      type:5,
      keyWords: '',
      pageSize: 3
    }).success(function (rs) {
      $scope.indexData.zxList = rs.data;
    });


    // 获取banner数据
    // utils.get('indexBannerList', {
    //   type:79,
    //   keyWords: '',
    //   pageSize: 3
    // }).success(function (rs) {
    //   $scope.indexData.bannerList = rs.data;
    //   $scope.pageInitData.bannerLoading = false;
    // });

    // 获取banner数据----------------
    utils.get('indexBannerList', {
      type:79,
      keyWords: '',
      pageSize: 3
    }).success(function (rs) {
      $scope.indexData.bannerXList = rs.data;
      $scope.pageInitData.bannerLoading = false;

      var _list = [];
      for (var i = 0; i < rs.data.length; i++) {
        _list.push({
          content: '<a href="/#/d/o/'+rs.data[i].id+'">\
            <img src="http://wx.ecloud-zj.com/ecloud'+rs.data[i].imageUrl+'" class="banner-loop">\
            <div class="banner-info">\
              <span  class="bl-title text-overflow">'+rs.data[i].title+'</span>\
            </div>\
          </a>'
        })
      }

      var S = new iSlider({
        dom: document.getElementById('iSlider-wrapper'),
        data: _list,
        isLooping: 1,
        isOverspread: 1,
        isAutoplay:true,
        animateTime: 800,
        fixPage: false,
        preventDefaultException: { tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT|A)$/ },
        plugins: ['dot'],
    });




    });

    // window.mySwipe = Swipe(document.getElementById('slider'));

    // 获取会展数据
    utils.get('indexExpoList', {
      mainType: '',
      name:"",
      year:2016,
      page:1,
      size:3,
      exhibitionSite:8
    }).success(function (rs) {
      $scope.indexData.expoList = rs.data.list;
    });

    $scope.qwIndex = 0;
    // 获取最新公告数据
    utils.get('indexBannerList', {
      type:2,
      keyWords: '',
      pageSize: 3
    }).success(function (rs) {
      $scope.indexData.quickWarning = rs.data;
      var _maxLength = rs.data.length;
      $interval(function() {
        if ($scope.qwIndex === _maxLength-1) {
          $scope.qwIndex = 0;
        } else {
          $scope.qwIndex = $scope.qwIndex + 1;
        }
      }, 2500);
    });

    // 获取url参数
    function getUrlVars() {
      var vars = {};
      var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,
      function(m,key,value) {
        vars[key] = value;
      });
      return vars;
    }

    // $scope.test = {
    //   url: window.location.href,
    //   code: 'codeinit',
    //   rs: 'rsinit',
    //   openID: 'openID'
    // }

    // 获取openId保存到cookie
    $scope.saveOpenId2local = function () {
      var code = getUrlVars()["code"];
      // $scope.test.code = code;
      utils.get('code2openId', {
        code: code || ''
      }).success(function (rs) {
        // $scope.test.rs = rs;
        if (rs.status === 200) {
          // $scope.test.openID = rs.data;

          $cookieStore.put('openID', rs.data);
        }
      });
    }

    $scope.saveOpenId2local();


  }
]);
